package fr.mathis.morpionstarwars;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

@SuppressLint("UseSparseArrays") public class HelpActivity extends Activity{

	private static final int MENU_QUIT_GAME = 0;
	private static final Integer SOUND_SABRE = 6;
    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;

	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.help);
		initSounds();
	}
	
    private void initSounds() {
      	 
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(SOUND_SABRE, soundPool.load(this, R.raw.sabre, 1));

}
    
    public void playSound(int sound) {
    	 
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		final boolean soundb = prefs.getBoolean("sound", true);	
		if(soundb)
		{
    	
	        AudioManager mgr = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
	        int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	        soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, 0, 1f);
		}
    }
    
    @Override
    public void onDestroy()
    {
    	super.onDestroy();
    	playSound(SOUND_SABRE);
    	
    }
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);
	    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
	    
	}
    
	/*MENU*/
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(0, MENU_QUIT_GAME, 0, "Quitter").setIcon(R.drawable.quitter);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case MENU_QUIT_GAME:
	    	finish();
	    	return true;
	    }
	    return false;
	}
}
